package com.i72.governance.service.helper;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.i72.governance.service.bean.ConfigFlushEvent;
import com.i72.governance.service.bean.ServiceConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executor;

@Component
@Slf4j
public class NacosConfigHelper implements ConfigHelper {

    private static Object LOCK = new Object();
    private static ConfigService configService;
    private static long TIMEOUT = 3000L;
    private static String DEFAULT_GROUP = "DEFAULT_GROUP";

    /**
     * 服务治理中间件配置信息
     */
    @Autowired
    private ServiceConfig serviceConfig;

    private ApplicationContext applicationContext;

    private ConfigService getConfigService() {
        if (configService == null) {
            synchronized (LOCK) {
                if (configService == null) {

                    try {
                        configService = NacosFactory.createConfigService(serviceConfig.getAddress());
                    } catch (Exception e) {
                        //log.error("nacos create error");
                    }
                }
            }
        }
        return configService;
    }


    @Override
    @PostConstruct
    public void initConfig() {
        this.getConfigService();
    }

    @Override
    public void watchConfig(String dataId, String group) {
        try {
            if(StringUtils.isEmpty(group)){
                group = DEFAULT_GROUP;
            }
            configService.addListener(dataId, group, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String configInfo) {

                    //发布自定义事件，用来指导容器刷新
                    ConfigFlushEvent flushEvent = new ConfigFlushEvent(applicationContext);
                    flushEvent.setConfig(configInfo);

                    applicationContext.publishEvent(flushEvent);

                }

            });
        }catch (Exception e){

        }

    }

    @Override
    public String getConfig(String dataId,String group) {
        if(StringUtils.isEmpty(group)){
            group = DEFAULT_GROUP;
        }
        try {
            return configService.getConfig(dataId, group, TIMEOUT);
        }catch (Exception e){
            return "";
        }
    }

    public void setServiceConfig(ServiceConfig serviceConfig) {
        this.serviceConfig = serviceConfig;
    }


    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}
